<template>
    <div class="course">
        <el-card shadow="never" class="!border-none">
            <!-- Header Start -->
            <el-alert
                title="温馨提示：1、专区活动能很方便的聚合需要促销的商品，形成各类专题页；2、新增专区活动，可设置专区在首页上架显示；3、活动专区可任意删除，请谨慎操作。"
                type="success"
                :closable="false"
            />
            <!-- Header End -->
            <el-form class="mt-4" label-width="80px" inline>
                <el-form-item label="状态">
                    <el-select v-model="formData.status">
                        <el-option label="全部" value></el-option>
                        <el-option label="开启" :value="1"></el-option>
                        <el-option label="关闭" :value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <div class="ml-[20px]">
                        <el-button type="primary" @click="resetPage">查询</el-button>
                        <el-button @click="resetParams">重置</el-button>
                    </div>
                </el-form-item>
            </el-form>
        </el-card>

        <el-card shadow="never" class="mt-4 !border-none">
            <!-- Main BtnGroup Start -->
            <div>
                <el-button
                    type="primary"
                    @click="$router.push(`subject/detail`)"
                    v-perms="['subject:add']"
                    >新增专区活动
                </el-button>
            </div>
            <!-- Main BtnGroup Start -->

            <!-- Main TableData Start -->
            <div class="mt-4">
                <el-table
                    ref="tableData"
                    :data="pager.lists"
                    style="width: 100%"
                    v-loading="pager.loading"
                >
                    <el-table-column type="selection" min-width="55" />
                    <el-table-column property="name" label="专区封面" min-width="180">
                        <template #default="scope">
                            <div class="flex col-center">
                                <el-image
                                    style="width: 58px; height: 68px"
                                    :src="scope.row.cover"
                                    :preview-src-list="[scope.row.cover]"
                                    :hide-on-click-modal="true"
                                    :preview-teleported="true"
                                    :fit="'contain'"
                                >
                                </el-image>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column property="name" label="专区名称" min-width="180" />
                    <el-table-column property="courseCount" label="课程数量" min-width="140" />
                    <el-table-column property="status" label="专区状态" min-width="140">
                        <template #default="scope">
                            <el-switch
                                v-perms="['subject:change']"
                                v-model="scope.row.status"
                                :active-value="1"
                                :inactive-value="0"
                                @change="handleStatusChange($event, scope.row.id)"
                            />
                        </template>
                    </el-table-column>
                    <el-table-column property="createTime" label="添加时间" min-width="140" />
                    <el-table-column property="address" label="操作" min-width="200" fixed="right">
                        <template #default="scope">
                            <div class="flex">
                                <router-link
                                    class="m-r-10"
                                    :to="{
                                        path: 'subject/detail',
                                        query: {
                                            id: scope.row.id
                                        }
                                    }"
                                >
                                    <el-button link type="primary" v-perms="['subject:edit']"
                                        >编辑</el-button
                                    >
                                </router-link>
                                <router-link
                                    class="m-r-10"
                                    :to="{
                                        path: 'subject_course',
                                        query: {
                                            id: scope.row.id
                                        }
                                    }"
                                >
                                    <el-button link type="primary" v-perms="['subject:course:list']"
                                        >专区课程</el-button
                                    >
                                </router-link>

                                <el-button
                                    @click="handleDelete(scope.row.id)"
                                    v-perms="['subject:del']"
                                    link
                                    type="primary"
                                    >删除</el-button
                                >
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <!-- Main TableData End -->

            <!-- Footer Pagination Start -->
            <div class="flex justify-end mr-2">
                <pagination v-model="pager" @change="getLists" />
            </div>
            <!-- Footer Pagination End -->
        </el-card>
        <layout-footer></layout-footer>
    </div>
</template>

<script lang="ts" setup>
import {
    apiSubjectLists, // 专区列表
    apiSubjectDel, // 专区删除
    apiSubjectChangeStatus // 活动状态
} from '@/api/application/application'
import Pagination from '@/components/pagination/index.vue'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import LayoutFooter from '@/layout/components/footer.vue'

/** Data Start **/
// 表单数据
const formData: any = ref({
    status: ''
})
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiSubjectLists,
    params: formData.value
})
/** Data End **/

/** Methods Start **/
/**
 * @description 专区状态
 */
const handleStatusChange = async (status: number, id: number) => {
    try {
        await apiSubjectChangeStatus({ id, status: status ? 1 : 0 })
        getLists()
    } catch (err) {
        console.log('专区状态修改=>', err)
    }
}

/**
 * @description 删除专区 || 专栏
 */
const handleDelete = async (id: number) => {
    try {
        await feedback.confirm('是否确认删除！')
        await apiSubjectDel({ id })
        getLists()
    } catch (err) {
        console.log('删除专区=>', err)
    }
}
/** Methods End **/

/** LifeCycle Start **/
onMounted(() => {
    getLists()
})
/** LifeCycle End **/
</script>

<style lang="scss">
.course {
    .ls-input {
        width: 170px;
    }
}
.el-alert--success.is-light {
    background-color: #edefff;
    color: #4a5dff;
}
</style>
